package com.jinxiangyang.controller;


import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.CouponInfo;
import com.jinxiangyang.service.CouponInfoService;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 优惠券信息 前端控制器
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@RestController()
@RequestMapping("/couponInfo")
public class CouponInfoController {

    @Autowired
    CouponInfoService couponInfoService;

    @GetMapping
    @OperateLog(title = "营销管理模块",businessType = BusinessType.OTHER,operatorType = OperatorType.ADMIN)
    public AjaxResult findAllCoupon(){
        return AjaxResult.success(couponInfoService.list());
    }

    @GetMapping("/{id}")
    @OperateLog(title = "营销管理模块",businessType = BusinessType.OTHER,operatorType = OperatorType.ADMIN)
    public AjaxResult findCouponById(@PathVariable Long id){
        return AjaxResult.success(couponInfoService.getById(id));
    }

    @PutMapping
    @OperateLog(title = "营销管理模块",businessType = BusinessType.UPDATE,operatorType = OperatorType.ADMIN)
    public AjaxResult updCouponById(@RequestBody CouponInfo couponInfo){
        boolean flag = couponInfoService.updateById(couponInfo);
        if (flag){
            return AjaxResult.success("修改成功");
        }else{
            return AjaxResult.error("修改失败");
        }
    }
    @DeleteMapping("/{id}")
    @OperateLog(title = "营销管理模块",businessType = BusinessType.DELETE,operatorType = OperatorType.ADMIN)
    public AjaxResult delCouponById(@PathVariable Long id){
        boolean flag = couponInfoService.deleteById(id);
        if (flag){
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败");
        }
    }

    @PostMapping("/{currentPage}/{pageSize}")
    @OperateLog(title = "营销管理模块",businessType = BusinessType.OTHER,operatorType = OperatorType.ADMIN)
    public AjaxResult findCouponByPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        return couponInfoService.findCouponByPage(currentPage,pageSize);
    }

    @PostMapping()
    @OperateLog(title = "营销管理模块",businessType = BusinessType.INSERT,operatorType = OperatorType.ADMIN)
    public AjaxResult saveCoupon(@RequestBody CouponInfo couponInfo){
        boolean flag = couponInfoService.saveCouponInfo(couponInfo);
        if (flag){
            return AjaxResult.success();
        }else{
            return AjaxResult.error("新增失败");
        }
    }
}

